$(function(){
  
 //树型表格加载
 $('#treegrid').treegrid({
  url : '../../api/nj_GetdepInfosBypid',
  method : "get",
  idField : 'id',
  treeField : 'text',
  rownumbers: true,
  singleSelect : true,
  columns : [[
    {
      title : '部门名称',
      field : 'text',
      width : 250,
    },
    {
      title : '部门编码',
      field : 'departmentcode',
      width : 150,
      align : 'center',
    },
    {
      title : '节点类型',
      field : 'type',
      width : 180,
      align : 'center',
    },
    {
      title : '详细地址',
      field : 'address',
      width : 183,
      align : 'center',
    },
    {
      title : '添加日期',
      field : 'addtime',
      width : 180,
      align : 'center',
      formatter:function(val,row){
          var now = new Date(val);
          return now.format("yyyy-MM-dd hh:mm:ss");
      }
    },
     {
      title : '添加人',
      field : 'adduser',
      width : 180,
      align : 'center',
    },
   
  ]],
  //点击右键时候发生
  onContextMenu : function(e, node){
    e.preventDefault();//屏蔽浏览器的菜单
    $(this).treegrid('unselectAll');  //清除所有选中项
    $(this).treegrid('select', node.id); //选中状态 
    $('#menu').menu('show', {
      left : e.pageX,
      top : e.pageY
    });
  }
 });

  //添加/修改事件
  $('#save').click(function() {
    var departmentname = $('#departmentname').val();//部门名称
    var departmentcode = $('#departmentcode').val();//部门编码
    var type = $('#type').combobox('getValue');//节点类型
    var address = $('#departmentForm').find('input[name=address]').val();
    if (departmentname == "" || departmentname == null || departmentcode == "" || departmentcode == null) {
      $.messager.alert('警告', '部门名称、部门代码不能为空！', 'warning');
    } else {
       var node = $('#treegrid').treegrid('getSelected');
       if(flag == 'add'){
            //前台保存
            $('#treegrid').treegrid('append',{
                parent:node.id ,
                data:[{
                    departmentname:$('#departmentForm').find('input[name=departmentname]').val(),
                    departmentcode:$('#departmentForm').find('input[name=departmentcode]').val(),
                    type:$('#departmentForm').find('input[name=type]').val(),
                    address:address,
                    addtime:getNowTime(),//获取当前时间
                    adduser:$.cookie("realname")
                }]
            });

              //后台添加
              $.ajax({
                type: "get",
                dataType: "json",
                url: "../../api/nj_saveDepartment",
                data: {
                  departmentname: departmentname,
                  departmentcode: departmentcode,
                  parentid:node.id,
                  address:address,
                  type:type,
                  adduser:$.cookie("realname")
                },

                success: function(r) {
                  if (r == 1) {
                    //刷新添加的节点信息
                    $('#treegrid').treegrid('reload', node.id);
                    $('#editinfo').dialog('close');
                  } else if (r == 2) {
                    $.messager.alert('警告', '部门名称已存在', 'warning');
                  } else {
                    $.messager.alert('错误', '部门添加失败', 'error');
                  }
                  }
              });
       }else{
              $.ajax({
                type: "get",
                dataType: "json",
                url: "../../api/nj_updateDeptById",
                data: {
                  departmentname: departmentname,
                  departmentcode: departmentcode,
                  id:node.id,
                  parentid:node.parent_id,
                  address:address,
                  type:type,
                  adduser:$.cookie("realname")
                },
                success: function(r) {
                  if (r == 1) {
                    //刷新添加的节点信息
                    $('#treegrid').treegrid('reload', node.parent_id);
                    $('#editinfo').dialog('close');
                  } else if (r == 2) {
                    $.messager.alert('警告', '部门名称已存在', 'warning');
                  } else {
                    $.messager.alert('错误', '部门修改失败', 'error');
                  }
                  }
              });
       }
       
    }
  });
  //取消按钮事件
  $("#cancel").click(function(){
    $('#editinfo').dialog('close');
  });

});


//1.添加部门
function addDept() {
  flag='add';
  //清空表单数据
  $('#departmentForm').form('clear');
  $('#editinfo').form('clear');
  //设置弹出框的位置
  var xposition = screen.width / 2 - 400;
  $('#editinfo').dialog('setTitle', '增加');
  $('#editinfo').window('open').window('move', {
    left: xposition,
    top: 50,
  });
  

};
//2.删除部门
function removeDept() {
  var node = $('#treegrid').treegrid('getSelected');
  $.messager.confirm('确定','您确定要删除吗？',function(t){
    if (t) {
      $.ajax({
        url : '../../api/nj_deleteDeptById?id=' + node.id,
        success : function(r) {
          if (r == 1) {
            $('#treegrid').treegrid('unselectAll');
            //删除完毕后整体reload---这个地方可以优化,现在是删除后整体reload的,体验不是很好   
            $('#treegrid').treegrid('remove', node.id);                    
            $('#treegrid').treegrid('reload',node.parent_id);
            $.messager.show({
              title:'提示信息',
              msg:'操作成功!'
            });
          } else if(r == 2) {
            $.messager.alert('警告', '该部门下有子部门，不能删除！', 'warning');
          } else {
            $.messager.alert('删除失败', r.msg, 'error');
          }
        }
      });
    }
  }).panel('move',{
    left:screen.width / 2 - 200,
    top:50
    })
};

//3.修改部门
function editDept() {
  flag='edit';
  var node = $('#treegrid').treegrid('getSelected');
  //设置弹出框的位置
  var xposition = screen.width / 2 - 400;
  $('#editinfo').form('clear');
  $('#editinfo').dialog('setTitle', '修改');
  $('#editinfo').window('open').window('move', {
    left: xposition,
    top: 50,
  });

  $("#editinfo").form("load", {
    departmentname: node.departmentname,
    departmentcode: node.departmentcode,
    address:node.address,
    type:node.type
  });
};




